package com.tjl.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjl.warehouse.exception.ThrowUtils;
import com.tjl.warehouse.mapper.ProductMapper;
import com.tjl.warehouse.mapper.StoreMapper;
import com.tjl.warehouse.mapper.UserInfoMapper;
import com.tjl.warehouse.model.InStore;
import com.tjl.warehouse.model.MyPage;
import com.tjl.warehouse.model.Product;
import com.tjl.warehouse.model.Store;
import com.tjl.warehouse.model.dto.InStoreReq;
import com.tjl.warehouse.service.InStoreService;
import com.tjl.warehouse.mapper.InStoreMapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Tang
* @description 针对表【in_store(入库单)】的数据库操作Service实现
* @createDate 2024-05-31 21:11:53
*/
@Service
public class InStoreServiceImpl extends ServiceImpl<InStoreMapper, InStore>
    implements InStoreService{

    @Resource
    private InStoreMapper inStoreMapper;
    @Resource
    private StoreMapper storeMapper;
    @Resource
    ProductMapper productMapper;
    @Resource
    UserInfoMapper userInfoMapper;

    @Override
    public MyPage<InStore> getPageList(InStoreReq req, MyPage<InStore> page) {
        ThrowUtils.throwIfParams(page == null);
        List<Integer> prodIds = null;
        //根据产品名 获取对应产品id列表
        if (StringUtils.isNotBlank(req.getProductName())){
            var productQuery = new LambdaQueryWrapper<Product>();
            productQuery.like(Product::getProductName, req.getProductName());
            prodIds = productMapper.selectList(productQuery)
                    .stream().map(Product::getProductId).toList();
        }

        String startTime = "1000-01-01 00:00:00";
        String endTime = "9999-12-30 23:59:59";
        if (StringUtils.isNotBlank(req.getStartTime())){
            startTime = req.getStartTime();
        }
        if (StringUtils.isNotBlank(req.getEndTime())){
            endTime = req.getEndTime();
        }

        var queryWrapper = new LambdaQueryWrapper<InStore>();
        //仓库id
        queryWrapper.eq(req.getStoreId() != null, InStore::getStoreId, req.getStoreId())
                //产品名
                .in(prodIds != null, InStore::getProductId, prodIds)
                //时间范围
                .between(!StringUtils.isAllBlank(req.getStartTime(),req.getEndTime()),
                        InStore::getCreateTime, startTime, endTime);

        //查询
        IPage<InStore> result = inStoreMapper.selectPage(page.toPage(), queryWrapper);
        //查询创建人
        result.getRecords().forEach(inStore -> {
            var user = userInfoMapper.selectById(inStore.getCreateBy());
            if (user != null)
                inStore.setUserCode(user.getUserCode());
        });
        //查询仓库名
        result.getRecords().forEach(inStore -> {
            var store = storeMapper.selectById(inStore.getStoreId());
            if (store != null)
                inStore.setStoreName(store.getStoreName());
        });
        //查询产品名
        result.getRecords().forEach(inStore -> {
            var product = productMapper.selectById(inStore.getProductId());
            if (product != null)
                inStore.setProductName(product.getProductName());
        });
        //设置入库价格
        result.getRecords().forEach(inStore -> {
            var product = productMapper.selectById(inStore.getProductId());
            if (product != null)
                inStore.setInPrice(product.getInPrice());
        });

        return new MyPage<>(result);
    }
}




